#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MENU_OFFSET 4

char *menu[] = 
{
    "a - add new record",
    "d - delete record",
    "s - select record",
    "f - format record",
    "q - quit",
    NULL
};

/*
typedef struct
{
	char * menuName;
	int (* menuFun)(char *menuBuffer);
}Menu_HandleTypeDef;


Menu_HandleTypeDef g_MenuHandle[] = 
{
	{"ls",    Cat_Command},
	{"ln",    Ln_Command},
	{"mkdir", Mkdir_Command},
	{"quit", Quit_Command},
	NULL,		
};
*/



int menuGetChoice(char *menuchoices[]);
void menuPrintChoice(int choice, char *menuchoices[]);

int main(int argc, char const *argv[])
{
    int choice = 0;

    do
    {
        choice = menuGetChoice(menu);
        menuPrintChoice(choice, menu);
    }while ( choice != 'q' );
    
    return 0;
}

int menuGetChoice(char *menuchoices[])
{
    int chosen = 0;
    int selected;
    char **option;

    do
    {
        printf("Choice:\n");
        option = menuchoices;
        while(*option)
        {
            printf("%s\n", *option);
            option++;
        }
        
        do
        {
            selected = getchar();
        } while ( selected == '\n');
        
        option = menuchoices;
        while(*option)
        {
            if( selected == *option[0])
            {
                chosen = 1;
                break;
            }
            option++;
        }

        if( !chosen)
        {
            printf("Incorrect choice, select again\n");
        }
    }while ( !chosen );

    return selected;

}


void menuPrintChoice(int choice, char *menuchoices[])
{
    char **option =  menuchoices;

    while( *option )
    {
        if( choice == *option[0] )
        {
            printf("%s is done\n", *option + MENU_OFFSET);
            break;
        }
        ++option;
    }

}